<!DOCTYPE HTML>
<html>
<head>
<title>Exit | AutoHotkey</title>
<meta name="description" content="The Exit command exits the current thread or (if the script is not persistent and contains no hotkeys) the entire script." />
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>Exit</h1>

<p>退出<a href="../misc/Threads.htm">当前线程</a>或整个脚本(如果脚本不是<a href="_Persistent.htm">持续运行的</a>且不包含热键时).</p>

<pre class="Syntax"><span class="func">Exit</span> <span class="optional">, ExitCode</span></pre>
<h2>参数</h2>
<dl>

  <dt>ExitCode</dt>
  <dd><p>一个介于 -2147483648 和 2147483647 之间的整数(可以为<a href="../Variables.htm#Expressions">表达式</a>), 在脚本退出时返回给它的调用者. 此代码可以被任何调用脚本的程序访问, 例如另一个脚本(使用 RunWait) 或批处理(.bat) 文件. 如果省略, 则 <em>ExitCode</em> 默认为零. 零一般用来表示成功.</p></dd>

</dl>

<h2>备注</h2>
<p>如果脚本中不含热键, 也不是<a href="_Persistent.htm">持续运行的</a>, 或没有让 <kbd>NumLock</kbd>/<kbd>ScrollLock</kbd>/<kbd>CapsLock</kbd> 键一直保持 AlwaysOn 或 AlwaysOff, 那么遇到 Exit 时它会立即终止(除非它含有 <a href="OnExit.htm">OnExit</a> 子程序).</p>
<p>在其他情况下, Exit 命令会终止<a href="../misc/Threads.htm">当前线程</a>. 换句话说, 由<a href="Menu.htm">菜单</a>, <a href="SetTimer.htm">计时器</a>或<a href="../Hotkeys.htm">热键</a>直接或间接调用的子程序堆栈全部都会像遇到 <a href="Return.htm">Return</a> 那样立即被返回. 如果直接用在这样的子程序内部而不是由它间接调用的某个子程序中, 那么此时 Exit 效果等同于 <a href="Return.htm">Return</a>.</p>
<p>使用 <a href="ExitApp.htm">ExitApp</a> 来完全终止<a href="_Persistent.htm">持续运行的</a>或包含热键的脚本.</p>
<h2>相关</h2>
<p><a href="ExitApp.htm">ExitApp</a>, <a href="OnExit.htm">OnExit</a>, <a href="Functions.htm">函数</a>, <a href="Gosub.htm">Gosub</a>, <a href="Return.htm">Return</a>, <a href="../misc/Threads.htm">线程</a>, <a href="_Persistent.htm">#Persistent</a></p>
<h2>示例</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>#z::
Gosub, Sub2
MsgBox, This MsgBox will never happen because of the EXIT.
return

Sub2:
Exit  <em>; 终止当前子程序以及调用它的子程序.</em></pre>
</div>

</body>
</html>